package test;

import java.util.List;

import com.sleepycat.db.DatabaseException;

import fr.cephb.joperon.core.Fid;
import fr.cephb.joperon.core.Operon;
import fr.cephb.joperon.core.bio.Assembly;
import fr.cephb.joperon.core.db.SnpIdsDB;
import fr.cephb.joperon.core.entities.SnpIds;
import fr.cephb.lang.Endian;

/*
 
 connection to operon in read only
 and open a SnpIds database and
 find the position for a given SNP
 
 */
public class Example010
	{
	public static void main(String[] args)
		{
		Operon operon=null;
		SnpIdsDB db= null;
		try {
			Endian.setDefault(Endian.CNG);
			//connect to operon in read only mode
			operon= Operon.newInstance();
			db= new SnpIdsDB(operon,Assembly.getDefaultAssembly());
			//check snpids has been created for this assembly
			if(!db.exists())
				{
				throw new DatabaseException("no database for this assembly "+db.getAssembly());
				}
			
			//open this database
			db.open();
			
			//find the 'SnpIds' of rs25
			List<SnpIds> list = db.get("rs25");
			for(SnpIds id:list)
				{
				System.out.println(id.getChromosome()+" "+id.getPosition());
				}
			
			/*some bases like snpids are just a key/value structure
			 so this can be simplified by */
			for(Fid fid: db.getFids("rs25"))
				{
				System.out.println(fid.getChromosome()+" "+fid.getPosition());
				}
			
			} 
		catch (Exception e) {
			//print error if any
			e.printStackTrace();
			}
		finally
			{
			//ALWAYS close the database
			if(db!=null) db.safeClose();
			//ALWAYS close operon
			if(operon!=null) operon.safeClose();
			}
		}
	}
